package com.luca.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luca.system.domain.SysOperLog;
import com.luca.system.mapper.SysOperLogMapper;
import com.luca.system.service.ISysOperLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 操作日志 服务层处理
 *
 * @author luca
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        baseMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        var query = new QueryWrapper<SysOperLog>();
        if (StringUtils.isNoneBlank(operLog.getTitle())) {
            query.like("title", operLog.getTitle());
        }
        if (null != operLog.getBusinessType()) {
            query.eq("business_type", operLog.getBusinessType());
        }
        if (!ObjectUtils.isEmpty(operLog.getBusinessTypes())) {
            query.in("business_type", operLog.getBusinessTypes());
        }
        if (null != operLog.getStatus()) {
            query.eq("status", operLog.getStatus());
        }
        if (StringUtils.isNoneBlank(operLog.getOperName())) {
            query.like("oper_name", operLog.getOperName());
        }
        if (!ObjectUtils.isEmpty(operLog.getParams().get("beginTime"))) {
            query.ge("oper_time", operLog.getParams().get("beginTime"));
        }
        if (!ObjectUtils.isEmpty(operLog.getParams().get("endTime"))) {
            query.le("oper_time", operLog.getParams().get("endTime"));
        }
        query.orderByDesc("oper_id");
        return baseMapper.selectList(query);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(operIds));
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return baseMapper.selectById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        baseMapper.cleanOperLog();
    }
}
